package com.stuManager.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stuManager.dao.UserDao;
import com.stuManager.factory.DaoFactory;
import com.stuManager.model.CommonResponseModel;
import com.stuManager.model.User;
import com.stuManager.util.UserTokenUtil;

import java.sql.SQLException;

/*
    LoginService
    处理登录接口的逻辑
    tokenLoginService(String token)  使用token自动登录，在登录页面会调用这个接口，如果已登录则自动跳转
    loginService(User user)          使用用户名和密码登录，会生成token并写入cookie
    logoutService(String token)      登出接口，将清除数据库和cookie中的token
 */
public class LoginService {
    private UserDao userDao;

    public LoginService() {
        this.userDao = DaoFactory.getUserDao();
    }

    /*
        tokenLoginService
        通过保存于cookie的token自动登录
     */
    public CommonResponseModel tokenLoginService(String token) {
        CommonResponseModel m = new CommonResponseModel();
        if (token == null || token.length()==0) {
            //token为空，返回error1, 不进行查询和跳转等
            m.setErrorCode(1);
            m.setMessage("token为空");
        }
        else {
            try {
                User user = this.userDao.seekToken(token);
                if (user == null) {
                    m.setErrorCode(2);
                    m.setMessage("未能查询到token");
                }
                else {
                    m.setErrorCode(0);
                    m.setMessage("登录成功");
                    m.setResult(user);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return m;
    }

    /*
        常规登录接口
     */
    public CommonResponseModel loginService(User user) throws SQLException {
        CommonResponseModel m = this.userDao.checkInUser(user);
        if (m.getErrorCode() == 0) {
            //登录成功
            User tu = (User) m.getResult();
            String token = UserTokenUtil.createToken(tu);
            int result = this.userDao.saveToken(tu, token);
            if (result == 0) {
                //出错了
                m.setErrorCode(1);
                m.setMessage("生成用户token出错");
            }
            else {
                //成功
                m.setErrorCode(0);
                JSONObject json = (JSONObject) JSON.toJSON(m.getResult());
                if (json == null) {
                    json = new JSONObject();
                }
                json.put("token", token);
                m.setResult(json);
            }
        }
        else {
            m.setErrorCode(2);
        }
        return m;
    }

    /*
        logoutService
        登出接口
     */
    public CommonResponseModel logoutService(String token) {
        CommonResponseModel m = new CommonResponseModel();
        if (token == null || token.length()==0) {
            m.setErrorCode(1);
            m.setMessage("token为空");
            return m;
        }
        try {
            User user = this.userDao.seekToken(token);
            if (user == null) {
                m.setErrorCode(2);
                m.setMessage("token有误");
                return m;
            }
            int row = this.userDao.removeToken(user.getUid());
            if (row <= 0) {
                m.setErrorCode(3);
                m.setMessage("登出失败，error:3");
                return m;
            }
            m.setErrorCode(0);
            m.setMessage("登出成功");
            return m;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return m;
    }
}
